package com.vince.hulk.character
{
import org.flixel.FlxObject;

/**
 * author yangwz
 */		
public class MediumCharacter extends Character
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>MediumCharacter</code> instance.
	 * 
	 */
	public function MediumCharacter(X:Number=0, Y:Number=0)
	{
		super(X, Y, null);
		loadGraphic(blobMediumImg, true, true, 85, 70);
		addAnimation("walk", [1, 2, 3, 4], 5, true);
		addAnimation("idle", [0, 5], 3, true);
		addAnimation("jump", [3], 1, false);
		addAnimation("noMerge", [8, 7], 3, true);
		_moveSpeed = 250;
		_jumpPower = 400;
		width = 54;
		offset.x = 15;
		return;
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Embed(source = "assets/HulkMedium.png")]
	private var blobMediumImg:Class;
	
	private var noMerge:int;
	//==========================================================================
	//  Methods
	//==========================================================================
	override public function merge(character:Character):Character
	{
		var largeCharacter:LargeCharacter = new LargeCharacter(x, y);
		largeCharacter.x = x - width - 5;
		largeCharacter.y = y + height - largeCharacter.height;
		if(isOverlapping(largeCharacter))
		{
			largeCharacter.x = x;
			if (isOverlapping(largeCharacter))
			{
				largeCharacter.y = y + height;
				if (isOverlapping(largeCharacter))
				{
					largeCharacter.kill();
					setCouldntMerge();
					character.setCouldntMerge();
					return null;
				}
			}
		}
		character.kill();
		kill();
		return largeCharacter;
	}
	
	override public function split() : Array
	{
		var result:Array = new Array();
		var smallCharacter:SmallCharacter = new SmallCharacter(x, y);
		smallCharacter.y = y + height - smallCharacter.height;
		var small:SmallCharacter = new SmallCharacter(x + width / 2, y + height - smallCharacter.height);
		result.push(smallCharacter, small);
		kill();
		return result;
	}
	
	override public function size():int
	{
		return 2;
	}
	
	override public function setCouldntMerge():void
	{
		noMerge = 80;
	}
	
	override public function update() : void
	{
		if (facing == RIGHT)
		{
			offset.x = 20;
		}
		else
		{
			offset.x = 11;
		}
		if (this.noMerge > 0)
		{
			play("noMerge");
			var mediumCharacter:MediumCharacter = this;
			var merge:int = this.noMerge - 1;
			mediumCharacter.noMerge = merge;
		}
		else if (velocity.x != 0 && isTouching(FlxObject.DOWN))
		{
			play("walk");
		}
		else if (!isTouching(FlxObject.DOWN))
		{
			play("jump");
		}
		else
		{
			play("idle");
		}
		super.update();
	}
}
}